package com.lemon.smartsafety.utils;

import android.content.Context;

import com.baidu.mapapi.cloud.CloudManager;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/19 9:31
 * 修改人：Fargo
 * 修改时间：2016/8/19 9:31
 * 修改备注：  检索城市
 */
public class PoiSearchCityUtil  implements
        OnGetPoiSearchResultListener {

    private Context context;
    private PoiSearch mPoiSearch = null;

    //页码
    private     int loadIndex = 0;
    //搜索每页条数
    private     int capacity = 20;
    //POI  基本检索返回结果
    private PoiResult poiResult=null;


    public PoiSearchCityUtil(Context context) {
        this.context = context;
    }

    private CallBackInterface mCallBackInterface=null;

    public CallBackInterface getmCallBackInterface() {
        return mCallBackInterface;
    }

    public void setmCallBackInterface(CallBackInterface mCallBackInterface) {
        this.mCallBackInterface = mCallBackInterface;
    }


    public int getLoadIndex() {
        return loadIndex;
    }

    public void setLoadIndex(int loadIndex) {
        this.loadIndex = loadIndex;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public PoiResult getPoiResult() {
        return poiResult;
    }

    public void setPoiResult(PoiResult poiResult) {
        this.poiResult = poiResult;
    }

    public interface CallBackInterface {
        public void callBackFunction(boolean isSuccess);

    }


    /**
     * 搜索事件
     *
     */
    public void searchCity(String city, String key, CallBackInterface callBackInterface) {
        if(CommonUtil.strIsEmpty(key)){
            return;
        }

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);

        mCallBackInterface=callBackInterface;
        mPoiSearch.searchInCity((new PoiCitySearchOption())
                .city(city).keyword(key).pageNum(loadIndex).pageCapacity(capacity));

    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        LogUtils.LOGE("搜索城市具体地址结果的返回：");

        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            /*Toast.makeText(context, "未找到更多位置信息", Toast.LENGTH_LONG)
                    .show();*/
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(false);
                onDestroy();
            }
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.LOGE("找到结果");
            poiResult=result;
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(true);
                onDestroy();
            }

            return;
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(false);
            }
        }
        LogUtils.LOGE("未找到结果");
        onDestroy();
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }


    public void onDestroy() {
        CloudManager.getInstance().destroy();
    }




}
